package com.lsh.oms.provider.service.shipping;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.alibaba.fastjson.JSONArray;
import com.lsh.base.common.model.CommonResult;
import com.lsh.oms.api.model.order.receipt.ReceiptHeadDto;
import com.lsh.oms.api.model.query.QueryShippingDto;
import com.lsh.oms.api.model.shipping.ModifyShippingExtDto;
import com.lsh.oms.api.model.shipping.ModifyTransDto;
import com.lsh.oms.api.model.shipping.SetDelayShippingDto;
import com.lsh.oms.api.service.shipping.IShippingRestService;
import com.lsh.oms.core.service.shipping.core.ShippingService;
import com.lsh.oms.core.service.tool.Criteria;
import com.lsh.oms.core.service.tool.Criterias;
import com.lsh.oms.provider.service.BaseService;
import com.lsh.oms.provider.utils.ValidationBeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

@Path("shippings")
@Service(protocol = "rest", timeout = 30000)
@Slf4j
public class ShippingRestServiceImpl extends BaseService implements IShippingRestService {

    @Resource
    private ShippingService shippingService;

    @GET
    @Produces({MediaType.APPLICATION_JSON})
    @Override
    public CommonResult query(@Context UriInfo uriInfo,
                              @DefaultValue("1") @QueryParam("page") long page,
                              @DefaultValue("20") @QueryParam("page-limit") long limit) {
        return CommonResult.success(shippingService.query(Criteria.of(uriInfo), page, limit));
    }

    @GET
    @Path("{id}")
    @Produces({MediaType.APPLICATION_JSON})
    @Override
    public CommonResult findById(@Context UriInfo uriInfo,
                                 @PathParam("id") Long id) {
        return CommonResult.success(shippingService.findById(id, Criterias.detailsRequired(Criteria.of(uriInfo))));
    }

    @POST
    @Path("{id}/receipt")
    @Consumes({MediaType.APPLICATION_JSON})
    @Produces({MediaType.APPLICATION_JSON})
    @Override
    public Response receipt(@PathParam("id") Long id, ReceiptHeadDto request) {
        return Response.ok(shippingService.receipt(id, request)).build();
    }

    @POST
    @Path("/tms/query")
    @Consumes({MediaType.APPLICATION_JSON})
    @Produces({ContentType.APPLICATION_JSON_UTF_8})
    @Override
    public CommonResult findByParam(QueryShippingDto queryShippingDto) {

        log.info("查询发货信息参数 " + queryShippingDto.toString());

        if (!checkParam(queryShippingDto)) {
            log.info("参数不能为空");
            return CommonResult.error("参数不能为空");
        }

        queryShippingDto.setIs_valid(1);
        return CommonResult.success(shippingService.findByParam(queryShippingDto, true));
    }

    /**
     * tms 发货单查询接口
     *
     * @param queryShippingDto 查询参数
     * @return 返回结果
     */
    @POST
    @Path("/tms/query/shippingnum")
    @Consumes({MediaType.APPLICATION_JSON})
    @Produces({ContentType.APPLICATION_JSON_UTF_8})
    @Override
    public CommonResult findNumByParam(QueryShippingDto queryShippingDto) {
        log.info("查询发货数量信息参数 " + queryShippingDto.toString());

        if (CollectionUtils.isEmpty(queryShippingDto.getRoute_ids())) {
            log.info("route_ids 参数不能为空");
            return CommonResult.error("route_ids 参数不能为空");
        }

        queryShippingDto.setIs_valid(1);

        return CommonResult.success(shippingService.findNumByParam(queryShippingDto));
    }

    /**
     * tms 修改线路司机信息
     *
     * @param modifyTransDto
     */
    @POST
    @Path("/tms/route/modify")
    @Consumes({MediaType.APPLICATION_JSON})
    @Produces({ContentType.APPLICATION_JSON_UTF_8})
    @Override
    public CommonResult modifyTransByRoute(ModifyTransDto modifyTransDto) {
        log.info("修改线路司机信息参数 " + modifyTransDto.toString());

        if (null == modifyTransDto) {
            log.info("参数不能为空");
            return CommonResult.error("参数不能为空");
        }

        ValidationBeanUtils.checkRequiredFields(modifyTransDto);

        return CommonResult.success(shippingService.updateTrans(modifyTransDto));
    }

    /**
     * tms 修改线路司机信息
     *
     * @param modifyShippingExtDto
     */
    @POST
    @Path("/tms/proof/modify")
    @Consumes({MediaType.APPLICATION_JSON})
    @Produces({ContentType.APPLICATION_JSON_UTF_8})
    @Override
    public CommonResult modifyExtByShippingId(ModifyShippingExtDto modifyShippingExtDto) {

        log.info("修改omsext信息参数 " + modifyShippingExtDto.toString());

        if (null == modifyShippingExtDto) {
            log.info("modifyShippingExtDto 参数不能为空");
            return CommonResult.error("modifyShippingExtDto 参数不能为空");
        }

        ValidationBeanUtils.checkRequiredFields(modifyShippingExtDto);

        return CommonResult.success(shippingService.updateOmsExt(modifyShippingExtDto));
    }

    /**
     * tms 设置延迟配置送
     *
     * @param setDelayShippingDto
     */
    @POST
    @Path("/tms/set/delaytype")
    @Consumes({MediaType.APPLICATION_JSON})
    @Produces({ContentType.APPLICATION_JSON_UTF_8})
    @Override
    public CommonResult setDelayShipping(SetDelayShippingDto setDelayShippingDto) {
        log.info("设置延迟配置送 " + setDelayShippingDto.toString());

        if (null == setDelayShippingDto) {
            log.info("参数不能为空");
            return CommonResult.error("参数不能为空");
        }

        ValidationBeanUtils.checkRequiredFields(setDelayShippingDto);

        return CommonResult.success(shippingService.setDelay(setDelayShippingDto));
    }

    @GET
    @Path("getShippingsByOrderIds")
    @Produces({MediaType.APPLICATION_JSON})
    @Override
    public CommonResult findByOrderids(@QueryParam("order_ids") String orderIds) {
        if (StringUtils.isEmpty(orderIds)) {
            log.info("orderIds 参数不能为空");
            return CommonResult.error(" orderIds 参数不能为空");
        }

        log.info("getShippingsByOrderIds para is " + orderIds);
        JSONArray orderArray = JSONArray.parseArray(orderIds);

        if (null == orderArray || orderArray.isEmpty()) {
            log.info("orderIds orderArray 参数不能为空");
            return CommonResult.error(" orderIds orderArray 参数不能为空");
        }

        return CommonResult.success(shippingService.findByOrderIds(orderArray));
    }

    private boolean checkParam(QueryShippingDto queryShippingDto) {

        boolean flag = false;
        if (null == queryShippingDto) {
            return false;
        }

        if (null == queryShippingDto.getPn()) {
            queryShippingDto.setPn(0);
        }

        if (null == queryShippingDto.getRn()) {
            queryShippingDto.setRn(100);
        }

        if (null != queryShippingDto.getAddress_id() && queryShippingDto.getAddress_id() > 1L) {
            queryShippingDto.setRn(1);
        }

        if (!CollectionUtils.isEmpty(queryShippingDto.getOrder_ids())) {
            return true;
        }

        if (!CollectionUtils.isEmpty(queryShippingDto.getRoute_ids())) {
            return true;
        }

        if (!CollectionUtils.isEmpty(queryShippingDto.getStatuses())) {
            return true;
        }

        if (null != queryShippingDto.getF_order_id()) {
            return true;
        }

        if (null != queryShippingDto.getTrans_uid()) {
            return true;
        }

        return flag;
    }
}
